using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;


namespace FeedFly
{
    /// <summary>
    /// Item viewing form. 
    /// </summary>
    public partial class ItemForm : Form
    {
        private FeedItem feedItem;
        private int folderId;
        private bool fullScreenSetting;
        
        public ItemForm()
        {
            InitializeComponent();
        }

        private void ItemForm_Load(object sender, EventArgs e)
        {
            // get the fullscreen setting from the database
            fullScreenSetting = Settings.FullScreenView;
            updateFullScreen();
        }

        public void SetFeedItem(int itemId, int folderId)
        {
            // see if requested Id is already loaded
            if (feedItem == null || feedItem.Id != itemId)
                feedItem = FeedItem.GetItem(itemId);

            // mark as read if it hasn't been read yet
            if (!feedItem.IsRead)
            {
                feedItem.IsRead = true;

                ThreadPool.QueueUserWorkItem(
                    new WaitCallback(markReadDB),
                    new FeedItem.MarkRequest(itemId, feedItem.FeedId, folderId, true));
            }

            this.folderId = folderId;

            loadPreview();
        }

        private void markReadDB(object mr)
        {
            // toggle read state in database
            FeedItem.Mark((FeedItem.MarkRequest)mr);
        }

        private void loadPreview()
        {            
            StringBuilder html = new StringBuilder(feedItem.Description.Length 
                + feedItem.Title.Length + feedItem.Link.Length + 200);

            html.Append("<h4>").Append(feedItem.Title).Append("</h4>");

            html.Append(feedItem.PubDate.ToLocalTime().ToString("g"));

            if(feedItem.Author != null && feedItem.Author.Length > 0)
                html.Append(" by ").Append(feedItem.Author);

            html.Append("<hr />").Append(feedItem.Description);

            webBrowser1.DocumentText = html.ToString();
        }

        private void loadOriginal()
        { 
            Process.Start(feedItem.Link, null);
        }

        private void fullScreenMenuItem_Click(object sender, EventArgs e)
        {
            //NOTE: when you go to full screen you don't have the menu anymore
            //      so now you can't get out of full screen.
            //      To overcome this, Soft Keys are being used to 
            //      bring back the menu.
            
            this.toggleFullScreen();

            ThreadPool.QueueUserWorkItem(
                new WaitCallback(this.toggleFullScreenDB));
        }
    
        private void markReadMenuItem_Click(object sender, EventArgs e)
        {
            // toggle read state
            feedItem.IsRead = !feedItem.IsRead;
            markReadMenuItem.Checked = feedItem.IsRead;

            // make sure it stays fullscreen unless it is turned off
            if (fullScreenSetting) goFullScreen();

            ThreadPool.QueueUserWorkItem(
                new WaitCallback(markReadDB),
                new FeedItem.MarkRequest(feedItem.Id, feedItem.FeedId, folderId, feedItem.IsRead));            
        }

        private void viewOriginalMenuItem_Click(object sender, EventArgs e)
        {
            // stop downloading any items to the preview screen.
            this.webBrowser1.Stop();
            loadOriginal();
            if (fullScreenSetting) goFullScreen();
        }

        private void viewPreviewMenuItem_Click(object sender, EventArgs e)
        {
            loadPreview();
            if (fullScreenSetting) goFullScreen();
        }

        private void backToListMenuItem_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Stop();
            this.webBrowser1.DocumentText = null;
            Screens.ItemList.Show();
            Hide();
        }

        private void ItemForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.Menu == null && (e.KeyCode == Keys.F1 || e.KeyCode == Keys.F2))
                this.exitFullScreen();
        }
  
        private void updateFullScreen()
        {
            if (fullScreenSetting)
                this.goFullScreen();
            else
                this.exitFullScreen();

            this.fullScreenMenuItem.Checked = fullScreenSetting;
        }

        private void toggleFullScreen()
        {
            fullScreenSetting = !fullScreenSetting;
            updateFullScreen();
        }

        // Added so this could be run in another thread
        private void toggleFullScreenDB(object o)
        {
           Settings.FullScreenView = fullScreenSetting;
        }
 
        private void goFullScreen()
        {
            this.WindowState = FormWindowState.Maximized;
            this.Menu = null;
        }

        private void exitFullScreen()
        {
            this.WindowState = FormWindowState.Normal;
            this.Menu = mainMenu1;
        }
    
        private void viewCurrentPageMenuItem_Click(object sender, EventArgs e)
        {
            if(this.webBrowser1.Url != null)
                Process.Start(webBrowser1.Url.AbsoluteUri, null);
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            viewCurrentPageMenuItem.Enabled = webBrowser1.CanGoBack;
        }
   
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (fullScreenSetting) this.goFullScreen();     
        }
     
        private void stopDownloadMenuItem_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Stop();
            this.stopDownloadMenuItem.Enabled = false;
        }

    }
}